package com.ecnu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.PageDomain;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.common.utils.OtherUtil;
import com.ecnu.common.utils.PageUtils;
import com.ecnu.common.utils.StringUtils;
import com.ecnu.common.utils.uuid.IdUtils;
import com.ecnu.system.domain.ProjectIndividualFile;
import com.ecnu.system.domain.dto.MyFileDTO;
import com.ecnu.system.mapper.ProjectIndividualFileMapper;
import com.ecnu.system.service.IProjectIndividualFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * ecnu_活动_资料表 服务实现类
 * </p>
 *
 * @author zdn
 * @since 2025-03-23
 */
@Service
public class ProjectIndividualFileServiceImpl
        extends ServiceImpl<ProjectIndividualFileMapper, ProjectIndividualFile>
        implements IProjectIndividualFileService {
    @Autowired
    private ProjectIndividualFileMapper projectIndividualFileMapper;

    protected void startPage(PageDomain pageDomain) {
        PageUtils.startPage(pageDomain);
    }

    protected String getId() {
        return IdUtils.fastSimpleUUID();
    }

    @Override
    public TableDataInfo getList(MyFileDTO dto, LoginUser loginUser) {
        LambdaQueryWrapper<ProjectIndividualFile> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(ProjectIndividualFile::getName, dto.getName());
        }
        wrapper.eq(ProjectIndividualFile::getDelFlag, "0");
        wrapper.eq(ProjectIndividualFile::getCreateUser, loginUser.getUserId().toString());
        wrapper.orderByDesc(ProjectIndividualFile::getCreateTime);
        startPage(dto.getPageDomain());
        List<ProjectIndividualFile> projectIndividualFiles = projectIndividualFileMapper.selectList(wrapper);
        return OtherUtil.tableSuccess(projectIndividualFiles);
    }

    @Override
    public AjaxResult edit(ProjectIndividualFile file, LoginUser loginUser) {
        try {
//            if (StringUtils.isBlank(file.getId())) {
                return mySave(file, loginUser);
//            } else {
//                return myUpdate(file, loginUser);
//            }
        } catch (Exception e) {
            log.error(file.getName()+" 保存错误\n" + e.getMessage());
            return AjaxResult.error("保存错误", e.getMessage());
        }
    }

//    private AjaxResult myUpdate(ProjectIndividualFile file, LoginUser loginUser) {
//        if (StringUtils.isBlank(file.getId())) {
//            return AjaxResult.error("文件编号缺失");
//        }
//        LambdaQueryWrapper<ProjectIndividualFile> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ProjectIndividualFile::getId, file.getId());
//        queryWrapper.eq(ProjectIndividualFile::getDelFlag, "0");
//        ProjectIndividualFile individualFile = projectIndividualFileMapper.selectOne(queryWrapper);
//        if (individualFile == null) {
//            return AjaxResult.error("文件不存在");
//        }
//        file.setUpdateUser(loginUser.getUserId().toString());
//        projectIndividualFileMapper.updateById(file);
//        //添加标签
//
//        return AjaxResult.success("保存成功");
//    }

    private AjaxResult mySave(ProjectIndividualFile file, LoginUser loginUser) {
        if (StringUtils.isBlank(file.getName())) {
            return AjaxResult.error("文件名缺失");
        }
        if (StringUtils.isBlank(file.getUrl())) {
            return AjaxResult.error("文件路径缺失");
        }

        file.setId(getId());
        file.setCreateUser(loginUser.getUserId().toString());
        int insert = projectIndividualFileMapper.insert(file);
        //添加标签

        return AjaxResult.success("保存成功");
    }
}
